<title>C Data types</title>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>C Data types.</h1>
</center>
<hr>
<p>

<h2>Variable definition</h2>
C has a concept of '<i>data types</i>' which are used to 
<a href="../glossary.html#definition">define</a> a variable
before its use. <p>
The definition of a variable will assign storage for the variable and 
define the type of data that will be held in the location.<p>
So what data types are available?
<p>
<table border=1 bgcolor=ivory width=80%>
<tr align=center>
<td><a href="#int">int</a>
<td><a href="#float">float</a>
<td><a href="#double">double</a>
<td><a href="#char">char</a>
<td><A HREF="../SYNTAX/void.html">void</A>
<td><A HREF="../SYNTAX/enum.html">enum</A>
</table>
<p>
Please note that there is not a boolean data type. C does not have the 
traditional view about logical comparison, but thats another story.<p>
<font color="brown">
Recent C++ compilers do have a <a href="../../CPLUSPLUS/SYNTAX/bool.html">boolean</a> datatype.
<p>
</font>
<hr>
<h2><a name="int">int - data type</h2>
<b>int</b> is used to define integer numbers.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>

    {
        int Count;
        Count = 5;
    }
</pre>
</td></tr></table>
</center>
<p>
<hr>
<h2><a name="float">float - data type</h2>
<b>float</b> is used to define floating point numbers.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>

    {
        float Miles;
        Miles = 5.6;
    }
</pre>
</td></tr></table>
</center>
<p>
<hr>
<h2><a name="double">double - data type</h2>
<b>double</b> is used to define BIG floating point numbers. It reserves twice
the storage for the number. On PCs this is likely to be 8 bytes.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>

    {
        double Atoms;
        Atoms = 2500000;
    }
</pre>
</td></tr></table>
</center>
<p>
<hr>
<h2><a name="char">char - data type</h2>
<b>char</b> defines characters.
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>

    {
        char Letter;
        Letter = 'x';
    }
</pre>
</td></tr></table>
</center>
<p>
<a name="modifier">
<hr>
<h2>Modifiers</h2>
The three data types above have the following modifiers. 
<p>
<ul>
<li>short
<li>long
<li>signed
<li>unsigned
</ul>

The modifiers define the amount of storage allocated to the variable.
The amount of storage allocated is not cast in stone. ANSI has the
following rules:<p>
<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr><td>
<pre>

        short int <=    int <= long int
            float <= double <= long double
</pre>
</td></tr></table>
</center>
<p>
What this means is that a 'short int' should assign less than or the same
amount of storage as an 'int' and the 'int' should be less or the same bytes
than a 'long int'. What this means in the real world is:
<p>
<table border=2 width="100%" bgcolor="ivory">
<tr><td>
<pre>

                 Type  Bytes  Bits                Range
</pre>
</td></tr>
<tr><td>
<pre>

            short int    2      16          -16,384 -> +16,383          (16kb)
   unsigned short int    2      16                0 -> +32,767          (32Kb)
         unsigned int    4      16                0 -> +4,294,967,295   ( 4Gb)
                  int    4      32   -2,147,483,648 -> +2,147,483,647   ( 2Gb)
             long int    4      32   -2,147,483,648 -> +2,147,483,647   ( 2Gb)
          signed char    1       8             -128 -> +127
        unsigned char    1       8                0 -> +255
                float    4      32
               double    8      64
          long double   12      96
</pre>
</td></tr></table>
<p>
These figures only apply to todays generation of PCs. Mainframes and 
midrange machines could use different figures, but would still comply
with the rule above.<p>
You can find out how much storage is allocated to a data type by using
the <a href="../SYNTAX/sizeof.html">sizeof</a> operator.
<a name="qualifier">
<hr>
<h2>Qualifiers</h2>
<p>
<ul>
<li><a href="../SYNTAX/const.html">const</a>
<li><a href="../SYNTAX/volatile.html">volatile</a>
</ul>

The <i>const</i> qualifier is used to tell C that the variable value can not 
change after initialisation.<p>

        const float pi=3.14159;

<p>
<i>pi</i> cannot be changed at a later time within the program.<p>
Another way to define constants is with the 
<a href="../SYNTAX/define_preprocessor.html">#define</a> preprocessor which
has the advantage that it does not use any storage (but who counts bytes
 these days?).<p>
<hr>
<h2>See also:</h2>
<a href="type_conv.html">Data type conversion</a>
<p>
<a href="storage_class.html">Storage classes.</a>
<p>
<a href="cast.html">cast</a>
<p>
<a href="../SYNTAX/typedef.html">typedef</a> keyword.

<hr>
<p>
<center>
<table border="2" width="80%" bgcolor="ivory">
<tr align=center>
<td width=25%>
<a href="../cref.html">Top</a>
</td><td width=25%>
<a href="../master_index.html">Master Index</a>
</td><td width=25%>
<a href="../SYNTAX/keywords.html">Keywords</a>
</td><td width=25%>
<a href="../FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
 
<hr>
<address><a href="../../address.html">Martin Leslie</a>
<script language="JavaScript">
<!--  //
document.write(document.lastModified);
// -->
</script>
</address>
